/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package defensive_practice_trw;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 *
 * @author rockero
 */
public class MainFrame extends javax.swing.JFrame {
    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        coutaTextField = new javax.swing.JTextField();
        nHabitTextField = new javax.swing.JTextField();
        nClientesTextField = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        mainFrameButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Ingresa los siguientes datos:");

        coutaTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coutaTextFieldActionPerformed(evt);
            }
        });

        nHabitTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nHabitTextFieldActionPerformed(evt);
            }
        });

        nClientesTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nClientesTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("# Habitaciones");

        jLabel3.setText("Couta x habitacion");

        jLabel4.setText("# Clientes teatro");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mainFrameButton.setText("OK");
        mainFrameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainFrameButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(coutaTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nClientesTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(mainFrameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(120, Short.MAX_VALUE)
                    .addComponent(nHabitTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(coutaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nClientesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15)
                        .addComponent(mainFrameButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(29, 29, 29))))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(35, 35, 35)
                    .addComponent(nHabitTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(84, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nClientesTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nClientesTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nClientesTextFieldActionPerformed

    private void nHabitTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nHabitTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nHabitTextFieldActionPerformed

    private void coutaTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coutaTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_coutaTextFieldActionPerformed

    private void mainFrameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainFrameButtonActionPerformed
        
        
        Defensive_Practice_TRW.nSala=Integer.parseInt(nHabitTextField.getText());
        Defensive_Practice_TRW.cuotaHabi=Integer.parseInt(coutaTextField.getText());
        Defensive_Practice_TRW.nClientesTeatro=Integer.parseInt(nClientesTextField.getText());
        
    
        
        if(Defensive_Practice_TRW.nClientesTeatro>Defensive_Practice_TRW.nClientesLista) {
            LimitClient aleraLimitClient=new LimitClient();
            aleraLimitClient.setVisible(true);
        }
        else{ 
            
           
                int m= (int) Math.floor(Math.random()*(Defensive_Practice_TRW.nClientesTeatro-((Defensive_Practice_TRW.nClientesTeatro/2.7)+1))+Defensive_Practice_TRW.nClientesTeatro/2.7);
                
              
                int []azar=new int[m];
                int alea=(int) Math.floor(Math.random()*(Defensive_Practice_TRW.nClientesLista-(1))+0);
                azar[0]= (int) Math.floor(Math.random()*(Defensive_Practice_TRW.nClientesLista-(1))+0);
             
                for(int i=1;i<m;i++){
                    for(int j=0;j<i;j++){
                        while(alea==azar[j]){
                            j=0;
                            alea=(int) Math.floor(Math.random()*(Defensive_Practice_TRW.nClientesLista-(1))+0);
                        }                           
                                 
                                 
                                 }
                    azar[i]=alea;          
                     
                             }
               
                
             FileWriter fichero = null;
             PrintWriter pw = null;
        try{
            fichero = new FileWriter("ganadores.txt");
            pw = new PrintWriter(fichero);
            int aux;
            for(int i=0;i<m;i++){   
                        aux=i;
                        pw.println(Defensive_Practice_TRW.clientesLista[azar[i]]); 
                       
                    }
                 fichero.close();
                 WinnersGeneratedOk alerGeneratedOk= new WinnersGeneratedOk();
                 
                 alerGeneratedOk.setVisible(true);
               

        }  catch (IOException ex) {
                ErrorGenerateWinners alertaErrorGenerateWinners=new ErrorGenerateWinners();
                alertaErrorGenerateWinners.setVisible(true);
            }      
               
       
       
         
            } 
       
        
            
            
       
       
       
       
       
       
       
       
    }//GEN-LAST:event_mainFrameButtonActionPerformed
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
      
        java.awt.EventQueue.invokeLater(new Runnable() {

          
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField coutaTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton mainFrameButton;
    private javax.swing.JTextField nClientesTextField;
    private javax.swing.JTextField nHabitTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the coutaTextField
     */
    public String getCoutaTextField() {
        return coutaTextField.getText();
    }

    /**
     * @param coutaTextField the coutaTextField to set
     */
    public void setCoutaTextField(javax.swing.JTextField coutaTextField) {
        this.coutaTextField = coutaTextField;
    }

    /**
     * @return the jLabel1
     */
    public javax.swing.JLabel getjLabel1() {
        return jLabel1;
    }

    /**
     * @param jLabel1 the jLabel1 to set
     */
    public void setjLabel1(javax.swing.JLabel jLabel1) {
        this.jLabel1 = jLabel1;
    }

    /**
     * @return the jLabel2
     */
    public javax.swing.JLabel getjLabel2() {
        return jLabel2;
    }

    /**
     * @param jLabel2 the jLabel2 to set
     */
    public void setjLabel2(javax.swing.JLabel jLabel2) {
        this.jLabel2 = jLabel2;
    }

    /**
     * @return the jLabel3
     */
    public javax.swing.JLabel getjLabel3() {
        return jLabel3;
    }

    /**
     * @param jLabel3 the jLabel3 to set
     */
    public void setjLabel3(javax.swing.JLabel jLabel3) {
        this.jLabel3 = jLabel3;
    }

    /**
     * @return the jLabel4
     */
    public javax.swing.JLabel getjLabel4() {
        return jLabel4;
    }

    /**
     * @param jLabel4 the jLabel4 to set
     */
    public void setjLabel4(javax.swing.JLabel jLabel4) {
        this.jLabel4 = jLabel4;
    }

    /**
     * @return the jPanel1
     */
    public javax.swing.JPanel getjPanel1() {
        return jPanel1;
    }

    /**
     * @param jPanel1 the jPanel1 to set
     */
    public void setjPanel1(javax.swing.JPanel jPanel1) {
        this.jPanel1 = jPanel1;
    }

    /**
     * @return the jPanel2
     */
    public javax.swing.JPanel getjPanel2() {
        return jPanel2;
    }

    /**
     * @param jPanel2 the jPanel2 to set
     */
    public void setjPanel2(javax.swing.JPanel jPanel2) {
        this.jPanel2 = jPanel2;
    }

    /**
     * @return the mainFrameButton
     */
    public javax.swing.JButton getMainFrameButton() {
        return mainFrameButton;
    }

    /**
     * @param mainFrameButton the mainFrameButton to set
     */
    public void setMainFrameButton(javax.swing.JButton mainFrameButton) {
        this.mainFrameButton = mainFrameButton;
    }

    /**
     * @return the nClientesTextField
     */
    public String getnClientesTextField() {
        return nClientesTextField.getText();
    }

    /**
     * @param nClientesTextField the nClientesTextField to set
     */
    public void setnClientesTextField(javax.swing.JTextField nClientesTextField) {
        this.nClientesTextField = nClientesTextField;
    }

    /**
     * @return the nHabitTextField
     */
    public String getnHabitTextField() {
        return nHabitTextField.getText();
    }

    /**
     * @param nHabitTextField the nHabitTextField to set
     */
    public void setnHabitTextField(javax.swing.JTextField nHabitTextField) {
        this.nHabitTextField = nHabitTextField;
    }

}




